package com.exam.ch8;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.AbstractTableModel;

public class TableModel2 implements ActionListener{

	JTable table = null;
	
	public TableModel2(){
		JFrame jFrame = new JFrame();
		JButton b1 = new JButton("数学老师");
		b1.addActionListener(this);
		JButton b2 = new JButton("学生阿呆");
		b2.addActionListener(this);
		
		JPanel panel = new JPanel();
		panel.add(b1);
		panel.add(b2);
		
		
		JTable table = new JTable(new MyTableModel(2));
		table.setPreferredScrollableViewportSize(new Dimension(550, 30));//设置表格的窗口大小
		
		JScrollPane scrollPane = new JScrollPane(table);
		jFrame.getContentPane().add(panel,BorderLayout.NORTH);
		jFrame.getContentPane().add(scrollPane,BorderLayout.CENTER);
		
		
		jFrame.setTitle("Simple Table");
		jFrame.pack();
		jFrame.setVisible(true);
		jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		
	}
	
	class MyTableModel extends AbstractTableModel{

		
		private static final long serialVersionUID = -2148751179848066249L;
		
		Object [][] p1 = {{"阿呆","1234",new Integer(66),new Integer(50),new Integer(116),new Boolean(false),new Boolean(false)}};
		
		String [] n1= {"姓名","学号","语文","数学","总分","及格","作弊"};
		
		Object [][] p2 = {{"阿呆","1234",new Integer(50),new Boolean(false),new Boolean(false),"01234"},
						{"阿瓜","1235",new Integer(75),new Boolean(true),new Boolean(false),"05687"}};
		
		String [] n2 = {"姓名","学号","数学","及格","作弊","电话"};
		
		//model为1 表示显示阿呆学生的成绩，model为2则显示数学老师的表格
		int model = 1;
		
		public MyTableModel(int i){
			model = i;
		}
		
		@Override
		public int getRowCount() {
			if(model ==1){
				return p1.length;
			}else{
				return p2.length;
			}
			
		}

		@Override
		public int getColumnCount() {
	
			if(model ==1){
				return n1.length;
			}else{
				return n2.length;
			}
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			if(model == 1){
				return p1[rowIndex][columnIndex];
			}
			else{
				return p2[rowIndex][columnIndex];
			}
			
		}
		
		@Override
		public String getColumnName(int column) {
			if(model == 1){
				return n1[column];
			}else{
				return n2[column];
			}
		}
		
		@Override
		public Class<?> getColumnClass(int columnIndex) {
			//返回列的数据类型
			return getValueAt(0, columnIndex).getClass();
		}
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		String cmd = e.getActionCommand();
		if(cmd.equals("数学老师")){
			table.setModel(new MyTableModel(2));
		}
		if(cmd.equals("学生阿呆")){
			table.setModel(new MyTableModel(1));
		}
		table.revalidate();
		
		
	}
	
	
	public static void main(String[] args) {
		
		new TableModel2();
	}

	
}
